import { createRouter, createWebHistory } from 'vue-router'

export const routerFiles = import.meta.glob(
  ['../views/**/index.vue', '!**/components/**'],
  { eager: true }
)
const routeList = []
const upperName = name =>
  name
    .split('/')
    .map(da => da.replace(/^\w/g, d => d.toUpperCase()))
    .join('')

for (const [key, component] of Object.entries(routerFiles)) {
  const path = key.replace('../views', '').replace('/index.vue', '')
  const isEdit = path.indexOf('/edit') > -1

  const name = upperName(path.replace('/', ''))
  component.default.name = name
  routeList.push({
    path: isEdit ? path.replace('/edit', '/edit/:id') : path,
    name,
    component: async () => component,
    props: isEdit,
    hidden: isEdit,
    meta: {},
  })
}

export const routes = routeList

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes,
})
export default router
